package cn.iocoder.an.module.business.controller.admin.mangeid;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.business.controller.admin.mangeid.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.mangeid.MangeIdDO;
import cn.iocoder.an.module.business.service.mangeid.MangeIdService;

@Tag(name = "管理后台 - 产品ID管理")
@RestController
@RequestMapping("/business/mange-id")
@Validated
public class MangeIdController {

    @Resource
    private MangeIdService mangeIdService;

    @PostMapping("/create")
    @Operation(summary = "创建产品ID管理")
    @PreAuthorize("@ss.hasPermission('business:mange-id:create')")
    public CommonResult<Long> createMangeId(@Valid @RequestBody MangeIdSaveReqVO createReqVO) {
        return success(mangeIdService.createMangeId(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品ID管理")
    @PreAuthorize("@ss.hasPermission('business:mange-id:update')")
    public CommonResult<Boolean> updateMangeId(@Valid @RequestBody MangeIdSaveReqVO updateReqVO) {
        mangeIdService.updateMangeId(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产品ID管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:mange-id:delete')")
    public CommonResult<Boolean> deleteMangeId(@RequestParam("id") Long id) {
        mangeIdService.deleteMangeId(id);
        return success(true);
    }


    @GetMapping("/get")
    @Operation(summary = "获得产品ID管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:mange-id:query')")
    public CommonResult<MangeIdRespVO> getMangeId(@RequestParam("id") Long id) {
        MangeIdDO mangeId = mangeIdService.getMangeId(id);
        return success(BeanUtils.toBean(mangeId, MangeIdRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得产品ID管理列表")
    @PreAuthorize("@ss.hasPermission('business:mange-id:query')")
    public CommonResult<List<MangeIdRespVO>> getMangeIdList(@Valid MangeIdListReqVO listReqVO) {
        List<MangeIdDO> list = mangeIdService.getMangeIdList(listReqVO);
        return success(BeanUtils.toBean(list, MangeIdRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出产品ID管理 Excel")
    @PreAuthorize("@ss.hasPermission('business:mange-id:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMangeIdExcel(@Valid MangeIdListReqVO listReqVO,
              HttpServletResponse response) throws IOException {
        List<MangeIdDO> list = mangeIdService.getMangeIdList(listReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "产品ID管理.xls", "数据", MangeIdRespVO.class,
                        BeanUtils.toBean(list, MangeIdRespVO.class));
    }

}